/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-23
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 工具类
 * 
 * @author springyounger
 */
public class LotteryUtil {
    private static Logger      logger                  = Logger.getLogger(LotteryUtil.class);
    //private static final String EMPTY_STRING            = "";

    private static final long  MILLISECONDS_A_DAY      = 24 * 3600 * 1000;

    //private static final long   MILLISECONDS_A_HOUR     = 3600 * 1000;

    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DEFAULT_DATE_FORMAT     = "yyyy-MM-dd";

    /**
     * 判断字符串为空
     * 
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return ((str == null) || (str.length() == 0));
    }

    /**
     * 去字符空格
     * 
     * @param str
     * @return
     */
    public static String trim(String str) {
        return str.trim();
    }

    /**
     * 检查是否是数字字符串
     * 
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }

        int length = str.length();

        for (int i = 0; i < length; i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }

        return true;
    }

    /**
     * 检查字符串是否为空
     * 
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
        int length;

        if ((str == null) || ((length = str.length()) == 0)) {
            return false;
        }

        for (int i = 0; i < length; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }

        return false;
    }

    /**
     * 在当前时间上加上days天
     * 
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    /**
     * 获得指定格式的当前时间
     * 
     * @param format
     * @return
     */
    public static String datetime(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(new Date());
    }

    /**
     * 获得指定格式的日期时间
     * 
     * @param date
     * @param format
     * @return
     */
    public static String datetime(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 字符串转为日期类型
     * 
     * @param date
     * @return
     */
    public static Date stringToDate(String date, String format) {
        try {
            if (isNotBlank(date) && isNotBlank(format)) {
                SimpleDateFormat formatter = new SimpleDateFormat(format);
                return formatter.parse(date);
            }
        } catch (ParseException e) {
            logger.error("日期转换失败!", e);
        }
        return null;
    }

    /**
     * 获取当前时间
     * 
     * @return
     */
    public static Date getToday() {
        String today = datetime(DEFAULT_DATE_FORMAT);
        return stringToDate(today, DEFAULT_DATE_FORMAT);
    }

    /**
     * 只取当前时间的日期部分，小时、分、秒等字段归零
     * 
     * @param date
     * @return
     */
    public static Date dateOnlyExt(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 返回两个时间相差的天数
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getQuot(Date startDate, Date endDate) {
        long quot = 0;
        quot = endDate.getTime() - startDate.getTime();
        quot = quot / MILLISECONDS_A_DAY;
        return (int) quot;
    }

    /**
     * 返回两个时间相差的天数
     * 
     * @param startDateStr
     * @param endDateStr
     * @param format
     * @return
     */
    public static int getQuot(String startDateStr, String endDateStr, String format) {
        long quot = 0;
        format = (format != null && format.length() > 0) ? format : DEFAULT_DATE_FORMAT;
        SimpleDateFormat ft = new SimpleDateFormat(format);
        try {
            Date date1 = ft.parse(endDateStr);
            Date date2 = ft.parse(startDateStr);
            quot = date1.getTime() - date2.getTime();
            quot = quot / MILLISECONDS_A_DAY;
        } catch (ParseException e) {
            logger.error("获取两个日期相差天数异常: ", e);
        }
        return (int) quot;
    }
}
